package utils;

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import jndi.JndiFactory;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class ServletListener implements ServletContextListener {

	private static Logger jLog = Logger.getLogger(ServletListener.class);

	
	@Override
	public void contextInitialized(ServletContextEvent event) {

		try {
			// Initialize Logging
			File logConfigFile = new File(event.getServletContext()
					.getRealPath(JndiFactory.getEnvironmentAsString("configPath"))
					+ "/log4j.properties");

			if (!logConfigFile.exists()) {
				System.out
						.println("ERROR: Log4j configuration file not found: "
								+ event.getServletContext().getRealPath(
										JndiFactory.getEnvironmentAsString("configPath"))
								+ "/log4j.properties");
			}

			else {
				String logpath = logConfigFile.getPath();
				PropertyConfigurator.configure(logpath);
			}

			jLog.info("ContextInitialized");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		jLog.info("ContextDestroyed");
	}

		

}
